<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@  taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@  taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.2.1.js"></script>
<link rel="stylesheet" href="/css/bootstrap.css" >
<script src="/js/bootstrap.js"></script>
</head>
<body>
<form id="insFrm" action="/list" method="post">
	<input type="hidden" name="pageNum" id="pageNum">
	<input type="hidden" id="cId" value="${bill.companyId }">
	<input type="hidden" id="bId" value="${bill.businessId }">
	<input type="hidden" id="dId" value="${bill.deptId }">
	发薪单名称:<input type="text" name="name" value="${bill.name }">
	公司:<select name="companyId" id="companyId" onchange="companyChange(this)"></select>
	事业部:<select name="businessId" id="businessId" onchange="businessChange(this)"></select>
	部门:<select name="deptId" id="deptId"></select>
	<button>搜索</button>
</form>
<input type="button" value="批量删除" onclick="deles()">
<a href="${pageContext.request.contextPath}/toAdd">添加</a>
<table class="table">
		<tr>
			<th>
				<input type="button" value="全选" onclick="qx()">/
				<input type="button" value="全不选" onclick="qbx()">
				<input type="button" value="反选" onclick="fx()">
			</th>
			<th>编号</th>
			<th>名称</th>
			<th>金额</th>
			<th>计税标准文件</th>
			<th>文件下载</th>
			<th>创建时间</th>
			<th>所属组织</th>
			<th>所属事业部</th>
			<th>所属部门</th>
			<th>操作</th>
		</tr>
	<c:forEach var="i" items="${pageInfo.list }">
		<tr>
			<th>
				<input type="checkbox" class="q" value="${i.id }">
			</th>
			<th>${i.code }</th>
			<th>${i.name }</th>
			<th>${i.acount }</th>
			<th>
				<img alt="没有图片" src="${i.bills }" width="60px" height="30px">
			</th>
			<th>
				<a download="xxx" href="${i.bills }">文件下载</a>
			</th>
			<th>
				<fmt:formatDate value="${i.createtime }" pattern="yyyy-MM-dd"/>
			</th>
			<th>${i.companyName }</th>
			<th>${i.businessName }</th>
			<th>${i.deptName }</th>
			<th>
				<input type="button" value="删除" onclick="dele(${i.id})">
				<a href="${pageContext.request.contextPath}/toUpdate?id=${i.id}">修改</a>
			</th>
		</tr>
	</c:forEach>
	<tr>
		<th colspan="12">
			<jsp:include page="/WEB-INF/view/page/page_one.jsp"></jsp:include>
		</th>
	</tr>
</table>
<script type="text/javascript">
	function page(pageNum){
		$("#pageNum").val(pageNum);
		$("#insFrm").submit();
	}

	function qx(){
		$(".q").each(function(){
			this.checked=true;
		})
	}

	function qbx(){
		$(".q").each(function(){
			$(this).prop("checked",false);
		})
	}

	function fx(){
		$(".q").each(function(){
			this.checked = !this.checked;
		})
	}

	function deles(){
		var ids = $(".q:checked").map(function(){
			return $(this).val()
		}).get().join();
		alert(ids);
		if(ids){
				if(confirm("确定要删除这些数据吗?")){
					$.post(
						"/deletes",
						{id:ids},
						function(msg){
							if(msg>0){
								alert("删除成功")
								location.reload();
							}else{
								alert("删除失败")
							}
						}
					)
				}else{
					alert("取消删除")
			}
		}
	}

	function dele(id){
		if(confirm("确定要删除这条数据吗?")){
			$.post(
				"/deletes",
				{id:id},
				function(msg){
					if(msg>0){
						alert("删除成功")
						location.reload();
					}else{
						alert("删除失败")
					}
				}
			)
		}else{
			alert("取消删除")
		}
	}

	$(function(){
		//初始化公司
		initCompany();
	})
	
	function initCompany(){
		var cId = $("#cId").val();
		$.post(
			"${pageContext.request.contextPath}/getOrgan?parentId=0",
			{parentId:0},
			function(msg){
				var str = '<option value="">==请选择==</option>';
				for(var i = 0;i<msg.length;i++){
					str+='<option value="'+msg[i].id+'">'+msg[i].name+'</option>'
				}
				$("#companyId").html(str);
				$("#companyId [value='"+cId+"']").attr("selected",true);
				companyChange($("#cId")[0]);
			},
			"json"
		)
	}
	function companyChange(thiz){
		var bId = $("#bId").val();
		var parentId = $(thiz).val();
		if(parentId!=null && parentId!=''){
			$.post(
					"${pageContext.request.contextPath}/getOrgan",
					{parentId:parentId},
					function(msg){
						var str = '<option value="">==请选择==</option>';
						for(var i = 0;i<msg.length;i++){
							str+='<option value="'+msg[i].id+'">'+msg[i].name+'</option>'
						}
						$("#businessId").html(str);
						$("#businessId [value='"+bId+"']").attr("selected",true);
						businessChange($("#bId")[0]);
					},
					"json"
				)
			}
		
	}
	function businessChange(thiz){
		var dId = $("#dId").val();
		var parentId = $(thiz).val();
		if(parentId!=null && parentId!=''){
			$.post(
					"${pageContext.request.contextPath}/getOrgan",
					{parentId:parentId},
					function(msg){
						var str = '<option value="">==请选择==</option>';
						for(var i = 0;i<msg.length;i++){
							str+='<option value="'+msg[i].id+'">'+msg[i].name+'</option>'
						}
						$("#deptId").html(str);
						$("#deptId [value='"+dId+"']").attr("selected",true);
					},
					"json"
				)
		}
	}
</script>
</body>
</html>